/**
 * ���ý���
 */
var fs = require('fs');
var async = require('async');
var NUMBER = 10;
var source = require('./source/testSource.js');
fs.readFile('investingTest.js', 'utf8', function (error, data) {
    if (error) throw error;
    console.log(data);
    var arr = [];
    var l = Math.floor(source.length / NUMBER);
    var dd = source.length % NUMBER;
    var idx = 0;
    for (var i = 0; i < l; i++) {
        var ar = [];
        ar.push(idx);
        idx += NUMBER;
        ar.push(idx);
        arr.push(ar);
    }
    if (dd) {
        var arrr = [];
        arrr.push(idx);
        idx += dd;
        arrr.push(idx);
        arr.push(arrr);
    }
    console.log(arr);
    var mmmm = 0;
    async.map(arr, function (item, cb) {
        mmmm++;
        var str = data.replace('****', item[0]);
        str = str.replace('####', item[1]);
        str = str.replace(/XXXXXXXXX/g, 'investingTest' + mmmm + '.js');
        fs.writeFile('investingTest' + mmmm + '.js', str, function (err) {
            if (err) return cb(err);
            cb(null, 'ok');
        });
    }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        }
        console.log(data.length);
        var ss = 'var cp = require("child_process");\n';
        for (var ii = 1; ii <= data.length; ii++) {
            console.log(ii);
            ss += 'var child' + ii + ' = cp.fork("./investingTest' + ii + '.js");\n';
        }
        fs.writeFile('main.js', ss, function (err) {
            if (err) throw(err);
            console.log('ok');
        });
    });
});
